<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="108px"
    >
      <el-form-item label="训练订单编码" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入训练订单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="关联训练产品ID" prop="exerciseProductId">
        <el-input
          v-model="queryParams.exerciseProductId"
          placeholder="请输入关联训练产品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="训练产品名称" prop="exerciseName">
        <el-input
          v-model="queryParams.exerciseName"
          placeholder="请输入训练产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="关联会员ID" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入关联会员ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="会员名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入会员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="训练周期以次为单位" prop="cycle">
        <el-input
          v-model="queryParams.cycle"
          placeholder="请输入训练周期以次为单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="会员手机号" prop="customerPhone">
        <el-input
          v-model="queryParams.customerPhone"
          placeholder="请输入会员手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入订单金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付类型" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付类型">
          <el-option
            v-for="item in payTypeList"
            :key="item.id"
            :label="item.payName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="订单实际支付金额" prop="payAmount">
        <el-input
          v-model="queryParams.payAmount"
          placeholder="请输入订单实际支付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="交易流水号" prop="transactionNo">
        <el-input
          v-model="queryParams.transactionNo"
          placeholder="请输入交易流水号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker
          clearable
          v-model="queryParams.payTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customer:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['customer:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['customer:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customer:order:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="训练订单表主键ID" align="center" prop="exerciseOrderId" />
      <el-table-column label="训练订单编码" align="center" prop="orderId" />
      <!-- <el-table-column label="关联训练产品ID" align="center" prop="exerciseProductId" /> -->
      <el-table-column label="训练产品名称" align="center" prop="exerciseName" />
      <el-table-column label="训练产品描述" align="center" prop="exerciseDesc" />
      <!-- <el-table-column label="关联会员ID" align="center" prop="customerId" /> -->
      <el-table-column label="会员名称" align="center" prop="customerName" />
      <el-table-column label="训练周期以次为单位" align="center" prop="cycle" />
      <el-table-column label="会员手机号" align="center" prop="customerPhone" />
      <el-table-column label="订单金额" align="center" prop="amount" />
      <el-table-column label="订单实际支付金额" align="center" prop="payAmount" />
      <!-- <el-table-column label="交易流水号" align="center" prop="transactionNo" /> -->
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支付类型(0微信 1支付宝 2银行卡)"
        align="center"
        prop="payType"
      />
      <el-table-column
        label="状态(0未支付 1已支付 2已退款 3已取消 4未完成)"
        align="center"
        prop="status"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['customer:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customer:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员训练订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="训练订单编码" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入训练订单编码" />
        </el-form-item>
        <el-form-item label="关联训练产品ID" prop="exerciseProductId">
          <el-input v-model="form.exerciseProductId" placeholder="请输入关联训练产品ID" />
        </el-form-item>
        <el-form-item label="训练产品名称" prop="exerciseName">
          <el-input v-model="form.exerciseName" placeholder="请输入训练产品名称" />
        </el-form-item>
        <el-form-item label="训练产品描述" prop="exerciseDesc">
          <el-input
            v-model="form.exerciseDesc"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <!-- <el-form-item label="关联会员ID" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入关联会员ID" />
        </el-form-item> -->
        <el-form-item label="会员名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入会员名称" />
        </el-form-item>
        <el-form-item label="训练周期以次为单位" prop="cycle">
          <el-input v-model="form.cycle" placeholder="请输入训练周期以次为单位" />
        </el-form-item>
        <el-form-item label="会员手机号" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入会员手机号" />
        </el-form-item>
        <el-form-item label="订单金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入订单金额" />
        </el-form-item>
        <el-form-item label="订单实际支付金额" prop="payAmount">
          <el-input v-model="form.payAmount" placeholder="请输入订单实际支付金额" />
        </el-form-item>
        <!-- <el-form-item label="交易流水号" prop="transactionNo">
          <el-input v-model="form.transactionNo" placeholder="请输入交易流水号" />
        </el-form-item> -->
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker
            clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
} from "@/api/customer/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员训练订单表格数据
      orderList: [],
      payTypeList: [
        { id: "Wechat", payName: "微信" },
        { id: "Alipay", payName: "支付宝" },
        { id: "BankPay", payName: "银行卡" },
        { id: "OtherPay", payName: "其他支付" },
        { id: "Experience5", payName: "体验支付" },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        exerciseProductId: null,
        exerciseName: null,
        exerciseDesc: null,
        customerId: null,
        customerName: null,
        cycle: null,
        customerPhone: null,
        amount: null,
        payAmount: null,
        transactionNo: null,
        payTime: null,
        payType: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [{ required: true, message: "训练订单编码不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员训练订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        response.rows.forEach((item) => {
          if (item.payType == "Wechat") {
            item.payType = "微信";
          } else if (item.payType == "Alipay") {
            item.payType = "支付宝";
          } else if (item.payType == "BankPay") {
            item.payType = "银行卡";
          } else if (item.payType == "OtherPay") {
            item.payType = "其他";
          } else if (item.payType == "Experience5") {
            item.payType = "5天体验";
          }

          if (item.status == "Unpaid") {
            item.status = "未支付";
          } else if (item.status == "Paid") {
            item.status = "已支付";
          } else if (item.status == "Refund") {
            item.status = "已退款";
          } else if (item.status == "Cancel") {
            item.status = "已取消";
          } else if (item.status == "Finish") {
            item.status = "已完成";
          } else if (item.status == "Exercise") {
            item.status = "训练中";
          } else {
            item.status = "失败";
          }
        });
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        exerciseOrderId: null,
        orderId: null,
        exerciseProductId: null,
        exerciseName: null,
        exerciseDesc: null,
        customerId: null,
        customerName: null,
        cycle: null,
        customerPhone: null,
        amount: null,
        payAmount: null,
        transactionNo: null,
        payTime: null,
        payType: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.exerciseOrderId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员训练订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const exerciseOrderId = row.exerciseOrderId || this.ids;
      getOrder(exerciseOrderId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员训练订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.exerciseOrderId != null) {
            updateOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const exerciseOrderIds = row.exerciseOrderId || this.ids;
      this.$modal
        .confirm('是否确认删除会员训练订单编号为"' + exerciseOrderIds + '"的数据项？')
        .then(function () {
          return delOrder(exerciseOrderIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "customer/order/export",
        {
          ...this.queryParams,
        },
        `order_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
